<template>
  <div class="app-maintain-container">
    <nu-col :span="24" style="width: 100%;">
      <nu-row class="row" :gutter="20" ref="formData">
        <nu-col :span="24" class="col">
          <span class="hint">两个细则考核辅助系统第十九条：按基本调峰考核，
            月最大考核电量不超过机组当月上网电量的 1%。</span><br />
          <span class="hint">若当月未维护上网电量数据，则默认为未超过机组当月上网电量的
            1%。</span>
        </nu-col>
        <nu-col :span="24" class="col">
          <nu-button type="primary" class="searchButton" @click="addTable">新增</nu-button>
        </nu-col>
        <nu-col :span="24">
          <div class="line"></div>
        </nu-col>
      </nu-row>
      <NuTableList :showFilter="false" :columns="columns" :dataSource="dataSource" :options="options"
        :operates="operates(this)" :pagination="pagination" @handleSizeChange="handleSizeChange"
        @handleChangePage="handleChangePage" @handleCurrentChange="handleCurrentChange" style="margin: 0 0 20px 0">
      </NuTableList>
    </nu-col>
    <nu-dialog :title="dialogTitle" :visible.sync="dialogShow" width="40%" center :close-on-click-modal="false" top="5vh"
      :modal-append-to-body="false" @close="diaClose">
      <div class="dialogHeaderSearch">
        <NuVerifyForm :model="dialogFormData" ref="dialogFormData" label-width="120px" class="submitForm"
          @submitForm="diaSubmitForm" :buttons="fyButtons">
          <NuformItem label="机组名称" prop="bidUnitId" width="70%" :rules="[{ required: true, message: '机组名称不能为空' }]">
            <nu-select class="searchSelect" :filterable="true" :list="nameList" type="text"
              v-model="dialogFormData.bidUnitId" autocomplete="off" style="width: calc(100% - 5px); margin-left: 2px">
            </nu-select>
          </NuformItem>
          <NuformItem label=" 月份" prop="monthDate" width="70%" :rules="[{ required: true, message: '时间不能为空' }]">
            <el-date-picker v-model="dialogFormData.monthDate" type="month" placeholder="请选择日期" style="width: 100%"
              value-format="yyyy-MM-01">
            </el-date-picker>
          </NuformItem>
          <NuformItem label="上网电量(MWh)" prop="gridEnergy" width="70%" :rules="[
            { required: true, message: '不能为空' },
            {
              pattern:
                /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
              message: '保留两位小数',
            },
          ]">
            <nu-input type="text" v-model="dialogFormData.gridEnergy" size="middle"></nu-input>
          </NuformItem>
        </NuVerifyForm>
        <nu-button class="dialogSearchButton" type="primary" @click="diaClose">取消</nu-button>
        <nu-button class="dialogSearchButton" type="primary" @click="diaSave">确定</nu-button>
      </div>
    </nu-dialog>
  </div>
</template>
<script>
import nuCol from "@/components/base/col.vue";
import nutbTitle from "@/components/base/tableTitle.vue";
import NuVerifyForm from "@/components/verify-form";
import NuformItem from "@/components/verify-form/form-item.vue";
import nuInput from "@/components/base/input";
import nuSelect from "@/components/base/select";
import nuButton from "@/components/base/button";
import NuTableList from "@/components/tableList/TableFormList";
import nuRow from "@/components/base/row.vue";
import nuDialog from "@/components/base/dialog";
import { getDateTime } from "@/utils/ruoyi";
const operates = (that) => [
  {
    label: "修改",
    icon: "",
    width: "50px",
    isShow: (row) => {
      return true;
    },
    disabled: (row) => {
      return false;
    },
    method: (row) => {
      that.openEdit(row);
    },
  },
  {
    label: "删除",
    icon: "",
    width: "50px",
    isShow: (row) => {
      return true;
    },
    disabled: (row) => {
      return false;
    },
    method: (row) => {
      that.tbRowdetele(row);
    },
  },
];
export default {
  name: "declare",
  components: {
    nuCol,
    nutbTitle,
    NuVerifyForm,
    NuformItem,
    nuInput,
    nuSelect,
    nuButton,
    NuTableList,
    nuRow,
    nuDialog,
  },
  data() {
    return {
      operates,
      currentRow: null,
      dataSource: [],
      columns: [
        {
          label: "序号",
          prop: "index",
          width: "70px",
          render: (h, params) => {
            return h('span', params.index + 1 + (this.pagination.pageNum - 1) * this.pagination.pageSize);
          },
        },
        {
          label: "机组名称",
          prop: "bidUnitName",
        },
        {
          label: "月份",
          prop: "monthDate",
          render: (h, params) => {
            return h('span', getDateTime('month', params.row.monthDate) );
          },
        },
        {
          label: "上网电量",
          prop: "gridEnergy",
        },
        {
          label: "记录维护时间",
          prop: "updateDate",
          render: (h, params) => {
            return h('span', getDateTime('', params.row.updateDate) );
          },
        },
        {
          label: "维护人",
          prop: "updateBy"
        }
      ],
      options: {
        "default-expand-all": true,
        "element-loading-background": "rgba(225,239,242)",
        "element-loading-text": "数据正在加载中",
        "row-key": "pid",
        "header-cell-style": {
          background: "rgba(105, 159, 209, 0.7)",
          color: "#fff",
          fontWeight: "bold",
          fontsize: "16px",
        },
        height: "calc(100vh - 385px)",
      },
      dialogTitle: "",
      dialogShow: false,
      dialogFormData: {
        bidUnitId: "",
        monthDate: "",
        gridEnergy: "",
      },
      nameList: [],
      fyButtons: [],
      pagination: {
        pageSize: 20,
        total: 0,
        pageNum: 1,
      },
    };
  },
  mounted() {
    this.getDeclareData();
    this.getBidUnitName();
  },
  methods: {
    /**
     * 获取表格数据
     */
    getDeclareData() {
      this.$httpGet(window.api.getMaintainData)
        .then((res) => {
          if (res.code == 200) {
            this.dataSource = res.rows;
            this.pagination.total = res.total
          } else {
          }
        })
        .catch((err) => { });
    },
    /**
     * 获取机组
     */
    getBidUnitName() {
      let postData = {
        phyunitName: "",
        pageSize: 9999,
        pageNum: 1,
      };
      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.$httpGet(window.api.getCrewManageTableData, postData)
        .then((res) => {
          if (res.code == 200 && res.rows) {
            res.rows.map((item) => {
              this.nameList.push({
                label: item.phyunitName,
                value: item.phyunitId,
              });
            });
          } else {
          }
        })
        .catch((err) => { });
    },
    /**
     * 新增
     */
    addTable() {
      this.dialogTitle = "新增";
      this.dialogShow = true;
    },
    /**
     * form组件内置查询事件
     */
    searchForm(val) {
      if (val) {
        this.getDeclareData();
      }
    },
    /**
     * 弹窗-保存
     */
    diaSave() {
      this.$refs["dialogFormData"].submitForm("form");
    },
    /**
     * 弹窗-关闭
     */
    diaClose() {
      this.dialogShow = false;
      this.resetForm("dialogFormData");
    },
    /**
     * 重置表单
     */
    resetForm(formName) {
      this.$refs[formName].resetForm("form");
      for (let k in this[formName]) {
        this[formName][k] = "";
      }
    },
    /**
     * 每页条数变化事件
     */
    handleSizeChange(val) {
      this.pagination.pageNum = 1;
      this.pagination.pageSize = val;
      this.getDeclareData();
    },
    /**
     * 改变分页触发事件
     */
    handleChangePage(val) {
      this.pagination.pageNum = val;
      this.getDeclareData();
    },
    /**
     * 单选
     */
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    /**
     * dialogFormData组件内置提交事件
     */
    diaSubmitForm(val) {
      if (val) {
        if (this.dialogTitle == "修改") {
          this.editElectric()
        } else if (this.dialogTitle == "新增") {
          this.addElectric()
        }
      }
    },
    /**
      * 新增接口
      */
    addElectric() {
      let postData = {
        ...this.dialogFormData,
      };

      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.$httpPost(window.api.addElectric, postData)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "成功",
              type: "success",
            });
            this.diaClose();
            this.getDeclareData();
          } else {
          }
        })
        .catch((err) => { });
    },
    /**
     * 表格-修改
     */
    openEdit(row) {
      this.dialogTitle = "修改";
      this.dialogShow = true;
      Object.assign(this.dialogFormData, row);
    },
    /**
     * 修改接口
     */
    editElectric() {
      let postData = {
        ...this.dialogFormData,
      };

      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.$httpPut(window.api.editElectric, postData)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "成功",
              type: "success",
            });
            this.diaClose();
            this.getDeclareData();
          } else {
          }
        })
        .catch((err) => { });
    },
    /**
     * 表格-删除
     */
    tbRowdetele(row) {
      if (!row.associatedChannel) {
        this.$confirm("确定要删除【" + row.bidUnitName + "】吗？", "确认删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.delElectric(row);
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    /**
     * 删除表格数据
     */
    delElectric(row) {
      this.$httpDel(window.api.delElectric, row.id)
        .then((res) => {
          if (res.code == 200) {
            this.getDeclareData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } else {
            this.$message({
              type: "warning",
              message: res.msg,
            });
          }
        })
        .catch((err) => { });
    },
  },
};
</script>
<style scoped>
.app-declare-container {
  height: calc(100vh - 70px);
}

.hint {
  display: block;
  font-size: 14px;
  line-height: 28px;
  color: rgba(79, 84, 112, 1);
}

.dialogHeaderSearch {
  background-color: var(--model-bgc);
  padding: 10px;
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: right;
}

.el-col {
  margin: 0;
}

.row {
  margin-left: 0 !important;
  margin-right: 0 !important;
}

.col {
  padding: 0px 10px 0px 10px;
}

.submitForm {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
</style>
